<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/20
 * Time: 16:14
 */

namespace app\api\controller;

use app\common\model\Bets;
use app\common\controller\Api;
use app\common\model\Config;
use think\Request;
use app\api\Service\GameService;

class Bet extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function detail(Request $request)
    {
        $gameService = new GameService();

        $detail = [];
        $id = $request->get('id');
        $game = $request->get('game');
        $odds = $gameService->getGameOdds($game.'odds');//赔率
        $bet = Bets::with("lottery{$game}")->where('user_id',$this->auth->id)->where('id',$id)//当前投注详情
        ->where('game',$game)
            ->find();

        if( !$bet ){
            $this->error('投注记录不存在！');
        }

        $bet['index'] = $bet['sequencenum'].'期';
        $gameDetail = json_decode($bet['result'],true);//开奖结果详情
        $bet['content'] = json_decode($bet['content'],true);//投注内容
        if( $gameDetail ){
            foreach ($gameDetail as $key=>$value){
                //筛选 投注内容
                foreach ($bet['content'] as $k=>$v){
                    if( $key == play_method($k) ){
                        $res = set_type_name($value);
                    }else{
                        unset($gameDetail[$key]);
                    }
                }

            }
        }
        $detail['result'] = isset($res) ? $res : '------';
        foreach ($bet['content'] as $k1=>$v1){
            $detail['play_method'] = $game;
            $detail['content']     = set_type_name($k1);
            $detail['money']       = $v1;
            $detail['odds']        = $odds[$k1];
        }

        $detail['index']     = $bet['index'];
        $detail['game']     = $bet['game_name'];
        $detail['order_sn']     = $bet['order_sn'];
        $detail['status']   = $bet['status_name'];
        $detail['zj_money']   = $bet['zj_money'];

        $detail['addtime']  = date('Y-m-d H:i:s',$bet['addtime']);
        if ( $bet['status'] != 3){

            $detail['lotterytime']  = $bet["lottery{$game}"] ? $bet["lottery{$game}"]['result_at'] : '------';
            $detail['sendtime'] = empty($bet['updatetime']) ? '------' : date('Y-m-d H:i:s',$bet['updatetime']);
        }


        $this->success('',$detail);

    }
}